package com.cicoding.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cicoding.core.ResponseResult;
import com.cicoding.dto.FileNotice;
import com.cicoding.utils.ResponseResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.cicoding.annotation.BizLog;
import com.cicoding.bean.Notice;
import com.cicoding.constant.Constant;
import com.cicoding.service.NoticeService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/notice")
public class NoticeController{
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private Gson gson;
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@ResponseBody
	@RequestMapping(value="/list" , method={RequestMethod.POST})
	public String noticeList(){
		 List<Notice> list = noticeService.getIndexNotice();
		 return gson.toJson(list);
	}
	@BizLog("添加通知")
	@ResponseBody
	@PostMapping(value="/add")
	public String addNotice(Notice notice){
//		String messagetype = request.getParameter("messagetype");
//		String body = request.getParameter("body");
		int result = Constant.RESULT_DEFAULT_ADD_NOTICE;
		if ("1".endsWith(notice.getMessage())) {
//			String recive = request.getParameter("recive");
//			result = noticeService.sendConmonNotice(body, recive);
		}else{
			result = noticeService.sendSystemNotice(notice);
		}
		
		return String.valueOf(result) ;
	}

	@ResponseBody
	@BizLog("添加通知")
	@PostMapping(value="/add.json")
	public ResponseResult addNotice2(Notice notice){
//		String messagetype = request.getParameter("messagetype");
//		String body = request.getParameter("body");
		int result = Constant.RESULT_DEFAULT_ADD_NOTICE;
		if ("1".endsWith(notice.getMessage())) {
//			String recive = request.getParameter("recive");
//			result = noticeService.sendConmonNotice(body, recive);
		}else{
			result = noticeService.sendSystemNotice(notice);
		}

		return ResponseResultUtil.renderSuccess(result);
	}

	@GetMapping(value="/noticeList.htm" )
	public String noticeListPage(HttpServletRequest request){
		List<Notice> list = noticeService.getIndexNotice();
		request.setAttribute("noticelist", list);
		return "notice/noticeList";
	}

	@RequestMapping(value = "/{id}.htm", method = { RequestMethod.GET })
	public String noticePage(@PathVariable("id") Integer id,HttpServletRequest request) {
		FileNotice fileNotice = noticeService.getFileNotice(id);
		System.out.println("-----------------------------------------fileNotice:"+fileNotice.toString());
		request.setAttribute("fileNotice",fileNotice);
		return "notice/notice";
	}

	@RequestMapping(value = "/addNotice.htm", method = { RequestMethod.GET })
	public String noticePage(HttpServletRequest request) {
		log.info("notice page");
		return "notice/addNotice";
	}
}
